﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.Model;
using Power.BLL;
using JN.Utility;
using DevExpress.XtraTreeList.Nodes;

namespace Power.MeterInfo
{
    public partial class StationInfo : DevExpress.XtraEditors.XtraForm
    {
        #region vaiable 
        private Station_infoBLL stationBLL = new Station_infoBLL();
        private bool isAdd;
        #endregion
        public StationInfo()
        {
            InitializeComponent();
        }

        private void StationInfo_Load(object sender, EventArgs e)
        {
            //load info
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据...", "Please Wait..."))
            {
                InitalizeUI();
                DisplayButton(false);
                EnanbleAllTextInput(false);
            } 
        }
        /// <summary>
        /// 初始化界面，加载treelist
        /// </summary>
        private void InitalizeUI()
        {
            try
            {
                TreeListNode node;
                treeList1.Nodes.Clear();
                node = treeList1.AppendNode(new object[] { "站点", "站点", null, null }, null);
                node.StateImageIndex = 1;
                //node2 = tlstHead.AppendNode(new object[] { "Step Name", "Step Name", null, null }, null);

                //get data from table
                DataTable dtData = stationBLL.GetAllList().Tables[0];
                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    node = treeList1.AppendNode(new object[] { dtData.Rows[i]["station_id"] + "_" + dtData.Rows[i]["station_name"], dtData.Rows[i]["station_id"], null, null }, 0);
                    node.StateImageIndex = 0;

                    //node2 = tlstHead.AppendNode(new object[] { dtStep.Tables[0].Rows[i]["step_name"], i, null, null }, 1);
                }
                //node.ExpandAll();
                treeList1.ExpandAll();
                this.treeList1.EndUnboundLoad();

                DisplayForm("");    //initalize list 
                                
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// 按键显示
        /// </summary>
        /// <param name="isAdd"></param>
        private void DisplayButton(bool isAdd)
        {
            try
            {
                if (isAdd)
                {
                    btnAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnQuery.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

                    btnSave.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnCancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                                        
                }
                else
                {
                    btnAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnQuery.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;

                    btnSave.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnCancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    
                    txtId.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// 输入状态
        /// </summary>
        /// <param name="isCanInput"></param>
        private void EnanbleAllTextInput(bool isCanInput)
        {
            try
            {
                txtId.Properties.ReadOnly = !isCanInput;
                txtName.Properties.ReadOnly = !isCanInput;
                txtRemark.Properties.ReadOnly = !isCanInput;
                                
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// 显示界面信息
        /// </summary>
        /// <param name="eqGrpID"></param>
        private void DisplayForm(string stationId)
        {
            try
            {                
                if (stationId != "")
                {
                    //get eq info                    
                    Model.Station_info mStation = stationBLL.GetModel(stationId);
                    if (mStation == null) return;                  
                    txtId.Text = mStation.station_id;
                    txtName.Text = mStation.station_name;
                    txtRemark.Text = mStation.remark;
                }                
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据...", "Please Wait..."))
            {
                InitalizeUI();
            } 
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                isAdd = true;
                int maxCount = stationBLL.GetRecordCount("");
                this.txtId.Text = maxCount.ToString("d4") ;
                this.txtName.Text = "";
                this.txtRemark.Text = "";
                //init param
                                
                DisplayButton(true);
                EnanbleAllTextInput(true);
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (txtId.Text.Trim() == "")
                {
                    XtraMessageBox.Show("请选择修改站点!!");
                    return;
                }
                isAdd = false;
                EnanbleAllTextInput(true);
                txtId.Enabled = false;
                DisplayButton(true);
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtId.Text == "")
            {
                XtraMessageBox.Show("请选择需要删除的站点");
                return;
            }
            DialogResult result = XtraMessageBox.Show("确认删除此站点定义??", "警告", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {                
                if (stationBLL.Delete(txtId.Text.Trim()))
                {
                    XtraMessageBox.Show("删除成功!!");
                }
                else
                {
                    XtraMessageBox.Show("删除失败!!");
                }
            }
            InitalizeUI();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (SaveData())
            {
                //refresh data
                XtraMessageBox.Show("保存成功!!");
                InitalizeUI();
                EnanbleAllTextInput(false);
                DisplayButton(false);
            }
            else
            {
                XtraMessageBox.Show("保存失败!!");
            }            
        }
       
        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DisplayButton(false);
            EnanbleAllTextInput(false);
        }

        private bool SaveData()
        {
            bool ret = false;
            try
            {
                if (txtId.Text == "")
                {
                    XtraMessageBox.Show("请输入站点编码!!");
                    return ret;
                }
                if (txtName.Text == "")
                {
                    XtraMessageBox.Show("请输入站点名称!!");
                    return ret;
                }
                if (isAdd)
                {                    
                    if (stationBLL.Exists(txtId.Text))
                    {
                        XtraMessageBox.Show("站点编码已经存在，请重新输入!!");
                        txtId.Focus();
                        return ret;
                    }
                    
                    //save param
                    Model.Station_info mSta = new Station_info();

                    mSta.station_id = txtId.Text;
                    mSta.station_name = txtName.Text;
                    mSta.remark = txtRemark.Text;

                    //add new step
                    ret = stationBLL.Add(mSta);
                    return ret;                                         
                }
                else
                {

                    Model.Station_info mSta = stationBLL.GetModel(txtId.Text);
                                        
                    mSta.station_name = txtName.Text;
                    mSta.remark = txtRemark.Text;

                    //add new step
                    ret = stationBLL.Update(mSta);
                    return ret;                      
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }

        private void treeList1_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSave.Visibility == DevExpress.XtraBars.BarItemVisibility.Always)
                {
                    XtraMessageBox.Show("正在修改模式，不能切换!");                    
                    return;
                }
                TreeListNode clickedNode = this.treeList1.FocusedNode;
                string disPlayText = clickedNode.GetDisplayText(0);
                if (disPlayText.ToUpper() != "站点")
                {
                    DisplayForm(disPlayText.Substring(0, clickedNode.GetDisplayText(0).IndexOf("_")));
                }
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
    }
}